// pages/order_detail/order_detail.js
// pages/comfirm_order/comfirm_order.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getVisitOrder, getArrivalOrder, cancelOrder, refundOrder, finishOrder, getOrderStep } from '../../static/api/api.js'
let payWay;
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_data:null,
        isOrderStepShow:false,
        order_type: '',
        orderStepData:'',
        order_id: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            'orderStatus': utils.orderStatus
        })
        this.setData({
            order_type: options.order_type,
            order_id: options.order_id
        })
        
        payWay = this.selectComponent("#payWay");
        this.setData({
            'orderStatus':utils.orderStatus
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (this.data.order_type == 10) {
            this.getVisitOrder()
        } else {
            this.getArrivalOrder()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    _hideDialog(){
        this.setData({
            isOrderStepShow:false
        })
    },
    //出现订单进度
    showOrderStep(){
        let options={
            order_id:this.data.order_data.order_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getOrderStep(options).then(response => {
            wx.hideLoading();
            this.setData({
                'isOrderStepShow': true,
                'orderStepData':response.data
            })

        }).catch(response => {
            wx.hideLoading();
        })
        
    },
    //获取上门订单详情
    getVisitOrder() {
        let options = {
            order_id: this.data.order_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getVisitOrder(options).then(response => {
            wx.hideLoading();
            this.setData({
                order_data: response.data
            })
            if (response.data.finish_time){
                this.setData({
                    'total_price': (parseFloat(this.data.order_data.payment_amount) + parseFloat(this.data.order_data.seconds_price)).toFixed(2)
                })
            }else{
                this.setData({
                    'total_price': (parseFloat(this.data.order_data.payment_amount)).toFixed(2)
                })
            }
            

        }).catch(response => {
            wx.hideLoading();
        })
    },
    //获取到店订单详情
    getArrivalOrder() {
        let options = {
            order_id: this.data.order_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getArrivalOrder(options).then(response => {
            wx.hideLoading();
            this.setData({
                order_data: response.data
            })
            this.setData({
                'total_price': parseFloat(this.data.order_data.payment_amount).toFixed(2)
            })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    cancelOrder(e) {
        let that = this;
        let dataset = e.currentTarget.dataset;
        let params = {
            order_id: dataset.id
        }
        wx.showModal({
            title: '提示',
            content: '确定取消当前订单吗？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    cancelOrder(params).then(response => {
                        wx.hideLoading()
                        wx.showToast({
                            icon:'none',
                            title: '订单取消成功',
                        })
                        setTimeout(function(){
                            wx.navigateBack()
                        },500)


                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })

    },
    orderPay() {
        let that = this;

        wx.login({
            success: res => {
                payWay._orderPayWay({
                    orderId: this.data.order_data.order_id,
                    code: res.code,
                    pay_type: 'wechat'
                }, function () {
                    setTimeout(function () {
                        wx.reLaunch({
                            url: '/pages/door_service_orders/door_service_orders',
                        })
                    })
                })


            }
        })
    },
    //用户退款
    refundOrder(e) {
        let that = this;
        let dataset = e.currentTarget.dataset;
        let params = {
            order_id: this.data.order_data.order_id
        }
        wx.navigateTo({
            url: `/pages/refund_detail_page/refund_detail_page?order_id=${this.data.order_id}&order_type=${this.data.order_type}`,
        })
        return;
        let msg = '';
        if (dataset.type == 'apply_refund') {
            msg = '确认要申请退款吗？'
        } else {
            msg = '确认要退款吗？'

        }
        wx.showModal({
            title: '提示',
            content: '确定要申请退款吗？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    refundOrder(params).then(response => {
                        wx.hideLoading()
                        if (dataset.type == 'apply_refund') {
                            wx.showToast({
                                icon: 'none',
                                title: '提交退款申请成功，请等待店铺确认',
                            })
                        } else {
                            wx.showToast({
                                title: '退款成功',
                            })

                        }
                        setTimeout(function(){
                            wx.navigateBack({
                                
                            })
                        },1000)


                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })
    },
    showPaySeconds(e) {
        let dataset = e.currentTarget.dataset;
        this.setData({
            'isShowDetail': true
        })
    },
    //触发支付
    orderSecondPay() {
        let that = this;
        wx.login({
            success: res => {
                payWay._orderPayWay({
                    orderId: this.data.order_data.order_id,
                    code: res.code,
                    payType: '2',
                    pay_type: 'wechat'
                }, function () {
                    wx.showToast({
                        icon: 'none',
                        title: '订单操作完成',
                    })
                    that.setData({
                        'order_data.status': 60
                    })
                })


            }
        })
    },
    cancelSecondPay() {
        this.setData({
            'isShowDetail': false
        })
    },
    comfirmOrderFinish(e) {
        let that = this;
        let dataset = e.currentTarget.dataset;
        let params = {
            order_id: this.data.order_data.order_id
        }
        wx.showModal({
            title: '提示',
            content: '确定完成当前订单吗？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    finishOrder(params).then(response => {
                        wx.hideLoading()
                        wx.showToast({
                            icon: 'none',
                            title: '订单操作完成',
                        })
                        setTimeout(function(){
                            wx.navigateBack({
                                
                            })
                        },1000)

                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })
    },
    makePhone(e){
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone,
        })
    }
})